﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TextReplace.Methods;
using TextReplace.Models;

namespace TextReplace
{
    public partial class 主界面 : Form
    {
        const string OldFolderPath = "oldFolderPath";
        DisplayModel displaymodel = new DisplayModel();
        public 主界面()
        {
            InitializeComponent();
        }

        private void btnChooseFolder_Click(object sender, EventArgs e)
        {
            txtPathName.Text = GetPath();
            if (string.IsNullOrWhiteSpace(txtPathName.Text))
            {
                return;
            }
            lblTIp.Text = "正在查找，请耐心等待。。。";
            btnBegin.Enabled = false;
            btnChooseFolder.Enabled = false;
            lblTIp.Enabled = false;
            TaskFactory tf = new TaskFactory();
            tf.StartNew(BeginSearch);

        }

        private void BeginSearch()
        {
            ReplaceHelper replaceHelper = new ReplaceHelper();
            displaymodel = replaceHelper.SearchFolderAndReplace(txtPathName.Text);
            Invoke(new Action(ShowResult));

        }

        private void ShowResult()
        {
            txtBefor.Text = displaymodel.BeforeContent.ToString();
            txtOver.Text = displaymodel.OverContent.ToString();
            var tip = $"查找完成，共找到{displaymodel.ReplaceModels.Count}个文件，{displaymodel.SearchRowCount}行代码。";
            lblTIp.Text = displaymodel.ExceptionContent.Length>0?$"{tip}有异常，点击查看": tip;
            btnBegin.Enabled = true;
            btnChooseFolder.Enabled = true;
            lblTIp.Enabled = true;
        }

        private string GetPath()
        {
           

            FolderBrowserDialog openDlg = new FolderBrowserDialog();
            openDlg.SelectedPath= ConfigHelper.GetAppSettings(OldFolderPath) ?? "";
            if (openDlg.ShowDialog() == DialogResult.OK)
            {
                var path = openDlg.SelectedPath;
                if (!string.IsNullOrWhiteSpace(path))
                {
                    ConfigHelper.AddOrUpdateAppSettings(OldFolderPath, path);
                }             
                return path;
            }
            else
            {
                return string.Empty;
            }
        }

        private void btnBegin_Click(object sender, EventArgs e)
        {
            if (displaymodel.ReplaceModels.Count<=0)
            {
                MessageBox.Show("没有需要替换的文件");
                return;
            }
            foreach (var model in displaymodel.ReplaceModels)
            {
                using (StreamWriter sw = new StreamWriter(model.FileName, false, model.FileEncoding))
                {
                    sw.Write(model.FileContent);
                }
            }
            displaymodel = new DisplayModel();
            MessageBox.Show("替换完成");
        }

        private void lblTIp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            TipForm tipForm = new TipForm();
            tipForm.displayModel = displaymodel;
            tipForm.Show();
        }
    }
}
